package com.wunu.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import com.wunu.entity.ImageMongo;
import com.wunu.service.IImageMgDbService;
import com.wunu.util.DateTimeUtil;
import com.wunu.vo.UserCmdIssuedVo;

@Component
public class ImageMgDbServiceImpl implements IImageMgDbService{
	 	@Resource
	 	private MongoTemplate mongoTemplate;
		/**
		 * *获取锁的所有人脸数据
		 * @param userId
		 * @param clientId
		 * @param type
		 * @return
		 */
		public  Map<String, Object>  getSendImageData(Integer pageIndex,Integer  pageSize,String clientId) {
			 List<UserCmdIssuedVo> userCmdIssueds = new ArrayList<UserCmdIssuedVo>();
			Map<String, Object> resultMap = new HashMap<>();
			//当前页码 默认给了0
		    pageSize = pageSize!=0?pageSize:10;
		    //页面展示数据条数 默认给了 10
		    int currentPage =pageIndex!=0?pageIndex:1;
		    //筛选条件
		    Query query = new Query().addCriteria(Criteria.where("clientId").is(clientId).and("endTime").gte(DateTimeUtil.nowDateTime()));
		    // 设置起始数 	// 设置查询条数
		    query.skip((currentPage - 1) * pageSize).limit(pageSize);
		    long totalCount = mongoTemplate.count(query,ImageMongo.class);
			// 数据总页数
			int totalPage = (int) (totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1);
			// 设置记录总数和总页数
			resultMap.put("totalCount", totalCount);
			resultMap.put("totalPage", totalPage);
			// 按创建时间倒序
			query.with(new Sort(Sort.Direction.DESC, "createTime"));
			// 查询当前页数据集合
			List<ImageMongo> ImageMongos = mongoTemplate.find(query, ImageMongo.class);
			for(ImageMongo imageMongo :ImageMongos) {
					 UserCmdIssuedVo userCmdIssuedVo = new UserCmdIssuedVo();
					 userCmdIssuedVo.setAvatar(imageMongo.getImageUrl());
					 userCmdIssuedVo.setNickName(imageMongo.getRemark());
					 userCmdIssuedVo.setPitNum(imageMongo.getPitNum());
					 userCmdIssuedVo.setUserId(imageMongo.getUserId());
					 userCmdIssuedVo.setEndTime(imageMongo.getEndTime());
					 userCmdIssuedVo.setStartTime(imageMongo.getStartTime());
					 userCmdIssuedVo.setValid(imageMongo.isValid());
					 userCmdIssuedVo.setSucc(imageMongo.isSucc());
					 if((imageMongo.isValid()&&imageMongo.isSucc())||imageMongo.getStatus()==1) {
						 userCmdIssuedVo.setStatus(1);
					 }else if((imageMongo.isValid()&&!imageMongo.isSucc())||imageMongo.getStatus()==0) {
						 userCmdIssuedVo.setStatus(0);
					 }else if((!imageMongo.isValid()||imageMongo.isSucc())||imageMongo.getStatus()==3) {
						 userCmdIssuedVo.setStatus(3);
					 }
					 userCmdIssuedVo.setData("");
					 userCmdIssueds.add(userCmdIssuedVo);
				 }
		    resultMap.put("content", userCmdIssueds);
			return resultMap;
		}
}
